package network.tprt2;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import network.*;
import user.LocalUser;

import controller.MessageController;
import controller.UserNotConnected;
import network.AbstractNetworkInterface;
import network.AbstractUserSignal;
import network.ListenerBindException;
import network.SendSignalException;
import network.config.NetworkConfiguration;

public class NetworkInterfaceRT2 extends AbstractNetworkInterface {

	public NetworkInterfaceRT2(MessageController msgCtrl,
			NetworkConfiguration netConf) throws ListenerBindException {
		super(msgCtrl, netConf);
	}

	@Override
	protected void performSignal(AbstractUserSignal asi)
			throws UnknownHostException, SendSignalException, UserNotConnected {
		UserSignalRT2 usrt = (UserSignalRT2)asi ;
		Message msg = (Message)usrt.getSignal() ;
		InetAddress addr = usrt.getAddress() ;
		switch (msg.getType_Message()) {
		case HELLO:
			Hello he = (Hello)msg ;
			super.msgCtrl.processHello (he.getNom_hote(), addr) ;
			break ;
		case HELLO_BACK:
			HelloBack her = (HelloBack)msg ;
			super.msgCtrl.processHelloReply (her.getNom_hote(), addr) ;
			break ;
		case BYE:
		case USER_KO:
			super.msgCtrl.processGoodBye (addr) ;
			break ;
		case CHAT_MESSAGE:
			ChatMessage st = (ChatMessage)msg ;
			super.msgCtrl.processSendText (st.getMess(), 
					new InetAddress[] { LocalUser.getLocalUser().getAddress()  }, addr) ;
			break ;
		default:
			throw new SendSignalException("Unrecognized signal : " + msg) ;
		}
	}

	@Override
	protected void addSignal(DatagramPacket packet) {
		addAbstractSignal(new UserSignalRT2(packet)) ;
	}

	@Override
	public void sendHello(String username) throws SendSignalException {
		super.sender.sendSignal(new UserSignalRT2(new Hello(username), null)) ;
	}

	@Override
	public void sendHelloReply(String username, InetAddress remoteAddress)
			throws SendSignalException {
		super.sender.sendSignal(new UserSignalRT2(new HelloBack(username), remoteAddress)) ;
	}

	@Override
	public void sendGoodBye() throws SendSignalException {
		super.sender.sendSignal(new UserSignalRT2(new Bye(LocalUser.getLocalUser().getUsername()), null)) ;
	}

	@Override
	public void sendAskLogin(InetAddress to) throws SendSignalException {
		super.sender.sendSignal(new UserSignalRT2(new Hello(LocalUser.getLocalUser().getUsername()), to)) ;
	}

	@Override
	public void sendMessage(String message, InetAddress to, InetAddress[] cc)
			throws SendSignalException {
		super.sender.sendSignal(new UserSignalRT2(new ChatMessage(message, LocalUser.getLocalUser().getUsername()), to)) ;
	}

	@Override
	public void sendPropFile(String filename, long fileSize, int fileID,
			InetAddress to) throws SendSignalException {
		throw new SendSignalException("Unsupported feature.") ;
	}

	@Override
	public void sendAcceptFile(InetAddress to, int fileID, boolean accepted,
			boolean now) throws SendSignalException {
		throw new SendSignalException("Unsupported feature.") ;
	}

}
